package com.fulong.service.impl;

import com.fulong.dao.SysMeunNagDao;
import com.fulong.entity.DeviceBaiseInfoEntity;
import com.fulong.entity.SysMeunNagEntity;
import org.apache.el.stream.Stream;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fulong.common.utils.PageUtils;
import com.fulong.common.utils.Query;
import com.fulong.service.SysMeunNagService;


@Service("sysMeunNagService")
public class SysMeunNagServiceImpl extends ServiceImpl<SysMeunNagDao, SysMeunNagEntity> implements SysMeunNagService {
    @Override
    public List<SysMeunNagEntity> withTree() {
        List<SysMeunNagEntity> sysMeunNagEntities = baseMapper.selectList(null);
        List<SysMeunNagEntity> collect  = sysMeunNagEntities.stream().filter(entiy -> entiy.getLevel() == 0)
                .map((el) -> {
                    el.setChildren(getChildren(el, sysMeunNagEntities));
                    return el;
                })
                .collect(Collectors.toList());
        return collect;
    }
    public List<SysMeunNagEntity> getChildren(SysMeunNagEntity parent ,List<SysMeunNagEntity> all) {
        return all.stream().filter(entiy->entiy.getParent().equals(parent.getId()))
                .map((childrent)->{
                    childrent.setChildren(getChildren(childrent , all));
                    return childrent;
                }).collect(Collectors.toList());
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysMeunNagEntity> page = this.page(
                new Query<SysMeunNagEntity>().getPage(params),
                new QueryWrapper<SysMeunNagEntity>()
        );
        return new PageUtils(page);
    }

}